package com.workcenter.controller.Sys;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Framework.Base.BaseController;
import com.common.annotation.Log;
import com.common.entity.Tree;
import com.common.utils.R;
import com.workcenter.entity.Sys.MenuDO;
import com.workcenter.service.MenuService;

@RequestMapping("/sys/menu")
@Controller
public class MenuController extends BaseController {
  String prefix = "sys/menu";
  @Autowired
  MenuService menuService;

  @RequiresPermissions("sys:menu:menu")
  @RequestMapping
  String menu(Model model) {
    return "sys/menu/menu";
  }

  @RequiresPermissions("sys:menu:menu")
  @RequestMapping("/list")
  @ResponseBody
  List<MenuDO> list() {
    List<MenuDO> menus = menuService.list();
    return menus;
  }

  @Log("添加菜单")
  @RequiresPermissions("sys:menu:add")
  @RequestMapping("/add/{pId}")
  String add(Model model, @PathVariable("pId") Long pId) {
    model.addAttribute("pId", pId);
    if (pId == 0) {
      model.addAttribute("pName", "根目录");
    } else {
      model.addAttribute("pName", menuService.get(pId).getName());
    }
    return "sys/menu/add";
  }

  @Log("编辑菜单")
  @RequiresPermissions("sys:menu:edit")
  @RequestMapping("/edit/{id}")
  String edit(Model model, @PathVariable("id") Long id) {
    model.addAttribute("menu", menuService.get(id));
    return "sys/menu/edit";
  }

  @Log("删除菜单")
  @RequiresPermissions("sys:menu:remove")
  @RequestMapping("/remove")
  @ResponseBody
  R remove(Long id) {
    if ("test" == getUsername()) {
      return R.error(1, "演示系统不允许删除,完整体验请部署程序");
    }

    if (menuService.remove(id) > 0) {
      return R.ok();
    } else {
      return R.error(1, "删除失败");
    }
  }

  @Log("保存菜单")
  @RequiresPermissions("sys:menu:add")
  @RequestMapping("/save")
  @ResponseBody
  R save(MenuDO menu) {
    if ("test" == getUsername()) {
      return R.error(1, "演示系统不允许删除,完整体验请部署程序");
    }

    if (menuService.save(menu) > 0) {
      return R.ok();
    } else {
      return R.error(1, "保存失败");
    }
  }

  @Log("更新菜单")
  @RequiresPermissions("sys:menu:edit")
  @RequestMapping("/update")
  @ResponseBody
  R update(MenuDO menu) {
    if ("test" == getUsername()) {
      return R.error(1, "演示系统不允许删除,完整体验请部署程序");
    }
    if (menuService.update(menu) > 0) {
      return R.ok();
    } else {
      return R.error(1, "更新失败");
    }
  }

  @RequestMapping("/tree")
  @ResponseBody
  Tree<MenuDO> tree() {
    Tree<MenuDO> tree = new Tree<MenuDO>();
    tree = menuService.getTree();
    return tree;
  }

  @RequestMapping("/tree/{roleId}")
  @ResponseBody
  Tree<MenuDO> tree(@PathVariable("roleId") Long roleId) {
    Tree<MenuDO> tree = new Tree<MenuDO>();
    tree = menuService.getTree(roleId);
    return tree;
  }
}
